<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class Identity extends BaseModel
{
    //定义表名
    protected $name = 'massage_service_user_identity';

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$field = '*'){

        $data = $this->where($dis)
            ->field($field)
            ->order('id desc')
            ->select()
            ->toArray();
        return $data;

    }

    //身份列表
    public function list($dis,$page = 10,$field = '*'){

        $data = $this->where($dis)
            ->field($field)
            ->order('id asc')
            ->paginate($page)
            ->toArray();
        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }

    //申请代言人
    public function spokesmanApply($input,$user_id)
    {
        $user_model = new User();
        //查找代言人身份ID
        $where = ['id' => $input['identity_id']];
        $spokesman_level = $this->dataInfo($where);
        if(empty($spokesman_level)){
            return ['code' => 500,'msg' => '未设置代言人身份'];
        }
        if(!empty($user_id)){
            $cap_dis[] = ['id','=',$user_id];
            $cap_info = $user_model->dataInfo($cap_dis);
        }else{
            $where = ['phone' => $input['phone'],'status' => 1];
            $cap_info = $user_model->dataInfo($where);
            if(empty($cap_info)){
                return ['code'=>500,'msg'=>'该手机号尚未注册'];
            }
        }
        if($cap_info['identity_id']){
            return ['code' => 500,'msg'=>'您已是其他身份'];
        }
        if($cap_info['identity_id'] == $spokesman_level['id']){
            return ['code' => 500,'msg'=>'您已经是代言人身份'];
        }
        $content = json_encode($input);
        $identity_list_model = new IdentityList();
        $where = [
            'user_id' => $cap_info['id'],
            'identity_id' => $spokesman_level['id'],
        ];
        $history = $identity_list_model->dataInfo($where);
        if($history){
            $data = [
                'content' => $content,
                'amount' => $spokesman_level['price'],
                'identity_id' => $input['identity_id'],
                'status' => 2,
            ];
            $identity_list_model->dataUpdate(['id' => $history['id']],$data);
            $list_id = $history['id'];
        }else{
            $data = [
                'user_id' => $cap_info['id'],
                'order_no' => 'OR'.uniqid().time(),
                'amount' => $spokesman_level['price'],
                'identity_id' => $spokesman_level['id'],
                'content' => $content,
                'status' => 2,
                'create_time' => time(),
            ];
            //添加身份申请记录表
            $list_id = $identity_list_model->insertGetId($data);
        }
        //修改用户信息
//        $res = $user_model->dataUpdate(['id' => $cap_info['id']],['identity_id' => $spokesman_level['id']]);

        return ['list_id' => $list_id];
    }

    //申请渠道商
    public function channelApply($input,$user_id,$uniacid)
    {
        $user_model = new User();
        //查找代言人身份ID
        $where = ['type' => 3,'status' => 1];
        $spokesman_level = $this->dataInfo($where);
        if(empty($spokesman_level)){
            return ['code' => 500,'msg' => '未设置渠道商身份'];
        }
        if(!empty($user_id)){
            $cap_dis[] = ['id','=',$user_id];
            $cap_info = $user_model->dataInfo($cap_dis);
        }else{
            $where = ['phone' => $input['mobile'],'status' => 1];
            $cap_info = $user_model->dataInfo($where);
            if(empty($cap_info)){
                return ['code'=>500,'msg'=>'该手机号尚未注册'];
            }
        }
        if($cap_info['identity_id']){
            return ['code' => 500,'msg'=>'您已是其他身份'];
        }
        if($cap_info['identity_id'] == $spokesman_level['id']){
            return ['code' => 500,'msg'=>'您已经是渠道商身份'];
        }
        $identity_list_model = new IdentityList();
        $coach_model = new Coach();
        $where = [
            'user_id' => $cap_info['id'],
            'status' => 1
        ];
        $history = $coach_model->dataInfo($where);
        if(isset($input['id'])){
            unset($input['id']);
        }
        $input['uniacid'] = $uniacid;
        $input['user_id'] = $user_id;
        //身份证
        $input['id_card'] = !empty($input['id_card'])?implode(',',$input['id_card']):'';
        //资质
        $input['license'] = !empty($input['license'])?implode(',',$input['license']):'';
        //体检报告
        $input['medical_report'] = !empty($input['medical_report'])?implode(',',$input['medical_report']):'';
        //无犯罪证明
        $input['not_guilty_certificate'] = !empty($input['not_guilty_certificate'])?implode(',',$input['not_guilty_certificate']):'';

        $input['self_img'] = !empty($input['self_img'])?implode(',',$input['self_img']):'';
        if($history){
            $coach_model->dataUpdate(['id'=>$cap_info['id']],$input);
        }else{
            $coach_model->dataAdd($input);
        }

        $where = [
            'user_id' => $cap_info['id'],
            'identity_id' => $spokesman_level['id'],
        ];
        $history = $identity_list_model->dataInfo($where);
        $content = json_encode($input);
        if($history){
            $data = ['content' => $content,'status' => 0];
            $identity_list_model->dataUpdate(['id' => $history['id'],$data]);
            $list_id = $history['id'];
        }else{
            $data = [
                'user_id' => $cap_info['id'],
                'order_no' => 'OR'.uniqid().time(),
                'amount' => $spokesman_level['price'],
                'identity_id' => $spokesman_level['id'],
                'content' => $content,
                'status' => 0
            ];
            //添加身份申请记录表
            $list_id = $identity_list_model->insertGetId($data);
        }
        return ['list_id' => $list_id];
    }
}